#coding:utf-8
from django.template import RequestContext
from django.http import HttpResponse,HttpResponseRedirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render
import json

import common.httpclient as http
import common.tips as tips
import common.modelAdminUser as modelAdminUser
import config.myurls as myurls
import common.function as myfc
import common.category as mycategory

import math

def all(req):
  '全部会员'
  
  
  main_nav=[]
  
  context={}
  context['title'] = '-全部会员'
  
  
  if modelAdminUser.getLoginUserId(req)==0:
    return HttpResponseRedirect('/login')
  
  url=myurls.ApiGetUserList;
  
  param=[];
  rt=[]
  
  
  param.append(("p",req.GET.get("page",1)))
  param.append(("pageSize",req.GET.get("pageSize",10)))
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  
  context['list']=rt['list']
  nav_list=[0]*math.ceil(int(rt['count'])/int(rt['pageSize']))
  paginator = Paginator(nav_list,1)
  current_page_num = req.GET.get('page',1)
  try:
    current_page = paginator.page(current_page_num)
  except PageNotAnInteger:
    current_page = paginator.page(1)
  except EmptyPage:
    current_page = paginator.page(paginator.num_pages)
  context['current_page']=current_page
  context['paginator']=paginator
  
  main_nav.append({"url":"","name":"全部会员("+str(rt['count'])+")"})
  context['main_nav']=main_nav
  
  return render(req, 'memberuser_list.html', context)

def realname(req):
  '实名会员'
  
  
  main_nav=[]
  context={}
  context['title'] = '-实名会员'
  
  if modelAdminUser.getLoginUserId(req)==0:
    return HttpResponseRedirect('/login')
  
  url=myurls.ApiGetUserList;
  
  param=[];
  rt=[]
  
  
  param.append(("p",req.GET.get("page",1)))
  param.append(("pageSize",req.GET.get("pageSize",10)))
  param.append(("is_realname",1))
  
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  
  context['list']=rt['list']
  nav_list=[0]*math.ceil(int(rt['count'])/int(rt['pageSize']))
  paginator = Paginator(nav_list,1)
  current_page_num = req.GET.get('page',1)
  try:
    current_page = paginator.page(current_page_num)
  except PageNotAnInteger:
    current_page = paginator.page(1)
  except EmptyPage:
    current_page = paginator.page(paginator.num_pages)
  context['current_page']=current_page
  context['paginator']=paginator
  
  main_nav.append({"url":"","name":"实名会员("+str(rt['count'])+")"})
  context['main_nav']=main_nav
  
  return render(req, 'memberuser_list.html', context)


def anonymous(req):
  '未实名会员'
  
  
  main_nav=[]
  context={}
  context['title'] = '-未实名会员'
  
  if modelAdminUser.getLoginUserId(req)==0:
    return HttpResponseRedirect('/login')
  
  url=myurls.ApiGetUserList;
  
  param=[];
  rt=[]
  
  
  param.append(("p",req.GET.get("page",1)))
  param.append(("pageSize",req.GET.get("pageSize",10)))
  param.append(("is_realname",2))
  
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  
  context['list']=rt['list']
  nav_list=[0]*math.ceil(int(rt['count'])/int(rt['pageSize']))
  paginator = Paginator(nav_list,1)
  current_page_num = req.GET.get('page',1)
  try:
    current_page = paginator.page(current_page_num)
  except PageNotAnInteger:
    current_page = paginator.page(1)
  except EmptyPage:
    current_page = paginator.page(paginator.num_pages)
  context['current_page']=current_page
  context['paginator']=paginator
  
  main_nav.append({"url":"","name":"未实名会员("+str(rt['count'])+")"})
  context['main_nav']=main_nav
  
  return render(req, 'memberuser_list.html', context)

def freeze(req):
  '已禁用会员'
  
  
  main_nav=[]
  context={}
  context['title'] = '-已禁用会员'
  
  if modelAdminUser.getLoginUserId(req)==0:
    return HttpResponseRedirect('/login')
  
  url=myurls.ApiGetUserList;
  
  param=[];
  rt=[]
  
  
  param.append(("p",req.GET.get("page",1)))
  param.append(("pageSize",req.GET.get("pageSize",10)))
  param.append(("login_status",2))
  
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  
  context['list']=rt['list']
  nav_list=[0]*math.ceil(int(rt['count'])/int(rt['pageSize']))
  paginator = Paginator(nav_list,1)
  current_page_num = req.GET.get('page',1)
  try:
    current_page = paginator.page(current_page_num)
  except PageNotAnInteger:
    current_page = paginator.page(1)
  except EmptyPage:
    current_page = paginator.page(paginator.num_pages)
  context['current_page']=current_page
  context['paginator']=paginator
  
  main_nav.append({"url":"","name":"已禁用会员("+str(rt['count'])+")"})
  context['main_nav']=main_nav
  
  return render(req, 'memberuser_list.html', context)
  
def edit(req):
  '会员编辑'
  
  id=0
  try:
    id=int(str(req.GET.get("id")))
  except ValueError:
    pass
  
  if id==0:
    return tips.error(req,"id不能为0","")
  
  main_nav=[]
  main_nav.append({"url":"javascript:window.history.go(-1);","name":"会员列表"})
  main_nav.append({"url":"","name":"会员编辑"})
  context={}
  context['title'] = '-会员编辑'
  context['main_nav']=main_nav
  
  url=myurls.ApiGetUserInfo;
  
  param=[];
  param.append(("id",id))
  rt=[]
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  
  context['info']=json.loads(rt['info'])
  return render(req, 'memberuser_edit.html', context)


def editpost(req):
  '提交编辑'
  
  rt=[]
  rt.append(("response_code",0))
  rt.append(("response_message","待提交"))
  
  param=[]
  
  for key in req.POST:
    param.append((key,req.POST.get(key)))
  
  
  url=myurls.ApiUpdUserInfo;
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  return HttpResponse(json.dumps(rt),content_type="application/json")
  #return tips.success(req,"提交成功","/memberuser/edit/?id="+req.POST.get("id"))
  